#!/bin/bash

source TestUtil.sh
source ../InitLinuxImpl.sh

#####################
# Test GetConfigVar #
#####################

TestVar_Debian_10=TheTestVarWithOSDebianVersion1.0
TestVar_Ubuntu=TheTestVarWithOSUbuntu
TestVar=TheTestVarValue

ErrorStr="Incorrect parameters for GetConfigVar!!!"

testName="Test GetConfigVar with No Parameter"
result=$(GetConfigVar)
ExpectAndEq "$result" "$ErrorStr" $? $PARAM_ERROR

testName="Test GetConfigVar with One Parameter"
result=$(GetConfigVar TestVar)
ExpectAndEq "$result" "$ErrorStr" $? $PARAM_ERROR

testName="Test GetConfigVar with One Variable Not Exist"
result=$(GetConfigVar TestVarNotExist)
ExpectAndEq "$result" "$ErrorStr" $? $PARAM_ERROR

testName="Test GetConfigVar with OS and Version Exist"
result=$(GetConfigVar TestVar Debian 10)
ExpectAndEq $result $TestVar_Debian_10 $? $SUCCESS

testName="Test GetConfigVar with OS Exist But NO Version"
result=$(GetConfigVar TestVar Ubuntu 10)
ExpectAndEq $result $TestVar_Ubuntu $? $SUCCESS

testName="Test GetConfigVar with NO OS NO Version"
result=$(GetConfigVar TestVar Fedora 10)
ExpectAndEq $result $TestVar $? $SUCCESS

testName="Test GetConfigVar with OS Exist"
result=$(GetConfigVar TestVar Ubuntu)
ExpectAndEq $result $TestVar_Ubuntu $? $SUCCESS

testName="Test GetConfigVar with OS Does NOT Exist"
result=$(GetConfigVar TestVar Suse)
ExpectAndEq $result $TestVar $? $SUCCESS

testName="Test GetConfigVar with Var Does NOT Exist But OS IS"
result=$(GetConfigVar TestVarNotExist Ubuntu)
ExpectAndEq "$result" "No variable in Config: TestVarNotExist!!!" $? $VAR_NOT_FOUND

#######################
# Test GetConfigArray #
#######################

TestArray=TestArrayList
TestArray_Debian=TestArrayList
TestArrayList=(1 2 3)

testName="Test GetConfigArray Without OS"
result=($(GetConfigArray TestArray Ubuntu))
ExpectAndEq "`echo ${result[@]}`" "1 2 3" $? $SUCCESS

testName="Test GetConfigArray With OS"
result=($(GetConfigArray TestArray Debian))
ExpectAndEq "`echo ${result[@]}`" "1 2 3" $? $SUCCESS

testName="Test GetConfigArray Without Define"
result=($(GetConfigArray TestArrayNotExist Debian))
ExpectAndEq $? $VAR_NOT_FOUND "`echo ${result[@]}`" "No variable in Config: TestArrayNotExist!!!"

######################
# Test Get Conf Paht #
######################

testName="Test GetYUMConfPath"
conf=/etc/yum.conf
if [ -f $conf ]
then
    result=$(GetYUMConfPath)
    ExpectAndEq $conf $result $? $SUCCESS
else
    result=$(GetYUMConfPath)
    ExpectAndEq $? $FILE_NOT_FOUND
fi

testName="Test GetDNFConfPath"
conf=/etc/dnf/dnf.conf
if [ -f $conf ]
then
    result=$(GetDNFConfPath)
    ExpectAndEq $conf $result $? $SUCCESS
else
    result=$(GetDNFConfPath)
    ExpectAndEq $? $FILE_NOT_FOUND
fi

testName="Test GetAPTConfPath"
conf=/etc/apt/apt.conf.d
if [ -d $conf ]
then
    result=$(GetAPTConfPath)
    ExpectAndEq $conf $result $? $SUCCESS
else
    result=$(GetAPTConfPath)
    ExpectAndEq $? $FILE_NOT_FOUND
fi

testName="Test GetVSFTPConfPath"
find /etc -name vsftpd.conf 2>/dev/null | grep vsftpd.conf
if [ $? == $SUCCESS ]
then
    result=$(GetVSFTPConfPath)
    ExpectAndEq $? $SUCCESS
else
    result=$(GetVSFTPConfPath)
    ExpectAndEq $? $FILE_NOT_FOUND
fi

testName="Test GetFTPUsersPath"
find /etc -name ftpusers 2>/dev/null | grep ftpusers
if [ $? == $SUCCESS ]
then
    result=$(GetFTPUsersPath)
    ExpectAndEq $? $SUCCESS
else
    result=$(GetFTPUsersPath)
    ExpectAndEq $? $FILE_NOT_FOUND
fi

echo

###########
# Summary #
###########

echo ">>>>>>>>>>>>>>"
echo Total Tests: ${totalTests}, Failed Tests: ${failedTests}
